package com.kuaiyin.dto;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.kuaiyin.enums.JmeterTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "JMeterTestDTO", description = "JMeter测试数据传输对象")
public class JMeterTestDTO {
    // ------------------------ 基础信息 ------------------------
    @Schema(description = "JMeter测试ID")
    private Long id;

    @Schema(description = "压测名称")
    private String name;

    @Schema(description = "压测计划关联的测试计划id")
    private Long planId;

    @Schema(description = "测试类型：SIMPLE(自定义)、JMX(上传脚本)", allowableValues = {"SIMPLE", "JMX"}, requiredMode = Schema.RequiredMode.REQUIRED)
    @TableField(value = "type")
    private JmeterTypeEnum type;

    @Schema(description = "压测备注")
    private String comment;

    @Schema(description = "JMeter上传的测试脚本内容")
    @TableField(exist = false)
    private String  jmxContent;

    @Schema(description = "响应提取配置:{\"1\":{\"token\":\"$.data.code\",\"id\":\"$.data.id\"},\"2\":{\"token\":\"$.data11.code\"}}")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private Map<String, Object> responseExtract;

    // ------------------------ 线程组配置 ------------------------
    @Schema(description = "线程组配置")
    private ThreadGroupConfig threadGroup;

    // ------------------------ 请求配置 ------------------------
    @Schema(description = "HTTP请求列表（至少需要一个请求）")
    private List<HttpRequestConfig> httpRequests;


    // ------------------------ 嵌套配置类 ------------------------

    /**
     * 线程组配置（与JMeter的ThreadGroup严格对应）
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "线程组配置")
    public static class ThreadGroupConfig {
        @Schema(description = "线程组名称（如'高并发场景'）", defaultValue = "Default Thread Group")
        private String name;

        @Schema(description = "并发用户数", defaultValue = "1")
        private int threads;

        @Schema(description = "启动时间（秒）", defaultValue = "0")
        private int rampUp;

        @Schema(description = "循环次数（-1表示永远）", defaultValue = "1")
        private int loops;

        @Schema(description = "是否使用调度器", defaultValue = "false")
        private boolean scheduler;

        @Schema(description = "持续时间（秒，scheduler=true时生效）")
        private int duration;

        @Schema(description = "延迟启动（秒）")
        private int delayedStart;
    }

    /**
     * HTTP请求配置（对应HTTPSamplerProxy）
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "HTTP请求配置")
    public static class HttpRequestConfig {
        @Schema(description = "请求名称（如'登录接口'）")
        private String name;

        @Schema(description = "协议", allowableValues = {"http", "https"}, defaultValue = "http")
        private String protocol;

        @NotBlank(message = "域名不能为空")
        @Schema(description = "域名/IP")
        private String domain;

        @Schema(description = "端口（0表示自动推导80/443）", defaultValue = "0")
        private int port;

        @Schema(description = "路径（如'/api/login'）", defaultValue = "/")
        private String path;

        @Schema(description = "请求方法", allowableValues = {"GET", "POST", "PUT", "DELETE", "PATCH"}, defaultValue = "GET")
        private String method;

        @Schema(description = "请求头")
        private Map<String, String> headers;

        @Schema(description = "URL查询参数")
        private Map<String, String> parameters;

        @Schema(description = "请求体（JSON/XML等）")
        private Map<String, Object> body;

        @Schema(description = "超时时间（毫秒）", defaultValue = "5000")
        private int timeout;

        @Schema(description = "是否跟随重定向", defaultValue = "true")
        private boolean followRedirects;

        @Schema(description = "是否保持连接", defaultValue = "true")
        private boolean keepAlive;

        @Schema(description = "响应提取变量")
        private Map<String, Object> extractVariables;

    }


}